



//
//  homeweimessage.swift
//  mywei
//
//  Created by myd on 15/12/19.
//  Copyright © 2015年 myd1. All rights reserved.
//

import UIKit
import SDWebImage
class homeweimessage: NSObject {
    var id : Int = 0
    var bmiddle_pic : String?
    var text : String?
    var source : String?
    var created_at : String?
    var users : homeweiuser?
    /// 转发数
    var reposts_count = 0
    /// 评论数
    var comments_count = 0
    /// 表态数
    var attitudes_count = 0
    private var pic = [NSURL]()
    var pic_urlss: [NSURL]?
        {
            return retweeted_status == nil ? pic : retweeted_status!.pic
    }
    
    var retweeted_status : homeweimessage?
    
    var pic_urls : [[String : AnyObject]]?{
        didSet{
       if pic_urls?.count==0
       {
        return
            }
//            let  arr  = NSMutableArray()
             pic = [NSURL]()
            for url in pic_urls!
            {
                
        let dic = url["thumbnail_pic"]
               
          let ur =  NSURL(string: dic! as! String)
                
                pic.append(ur!)
            }
           
        }
    }
    
 
    class func homeweimessage1 (dic : [String : AnyObject] ) -> NSMutableArray! {
        
        let dar = dic["statuses"] as? NSArray
        let aa = NSMutableArray()
        for vv in dar!
        {
            let v1 = vv as! [String : AnyObject]
            let aaa = homeweimessage(dic: v1)
            aa.addObject(aaa)
        }
        
        return aa
    }
    
    class func loaddate(since_id : Int , max_id : Int ,   finsh : (resuit : NSMutableArray? , Error : NSError?) -> ())
    {
        
   OauthNetwork.sharedinstance.loadweibomessage(since_id  , max_id : max_id) { (resuit, Error) -> () in
    if Error != nil || resuit == nil
    {
   finsh(resuit: nil, Error: Error!)
        return
    }
  
    let re = resuit! as [String : AnyObject]
  let  aa =  homeweimessage.homeweimessage1(re)
    finsh(resuit: aa, Error:nil)
        }
    }
    
    
    
    
    
    func cachedmessage(sta : [homeweimessage] , finsh : (re : [homeweimessage]? , error : NSError?)->())
    {
        var length = 0
        let group = dispatch_group_create()
   for ss in sta
   {
    guard let s1 = ss.pic_urlss else
    {
   continue
    }
    if s1.count == 1
    {
   let url = s1[0]
        dispatch_group_enter(group)
        SDWebImageManager.sharedManager().downloadImageWithURL(url, options: SDWebImageOptions(rawValue: 0), progress: nil, completed: { (image, error, _, _, _) -> Void in
            if error != nil
            {      finsh(re: nil, error: error)
           print("下载图片失败")
                return
            }
            let data = UIImagePNGRepresentation(image)
            length += data!.length
        })
        
    }}
    dispatch_group_notify(group, dispatch_get_main_queue()) { () -> Void in
        print("图片大小\(CGFloat(length))")
        finsh(re: sta, error: nil)
        }
    
    }
//
    override func setValue(value: AnyObject?, forKey key: String) {
        
        if key == "user"
        {
            if let dd = value as? [String : AnyObject] {
               let value1 = homeweiuser(dic: dd)
            self.users = value1
            return
            }}
   
            if key == "retweeted_status"
            {          
                if  let values = value as? [String : AnyObject]{
         retweeted_status = homeweimessage(dic: values)}
                return
            }
        
        super.setValue(value, forKey: key)
    }
    
    
    init(dic : [String : AnyObject])
    {
        super.init()
        setValuesForKeysWithDictionary(dic)
    }
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    func isor() -> String
    {
        return retweeted_status == nil ? statuscellre.homecellorg.rawValue : statuscellre.homeccell.rawValue
    }
}
